<?php
/**
 * Each engineer has a duty to keep the code elegant
 * Created by wolffy.
 * Author @wolffy<13750088090@sina.cn>
 * Date: 2018/8/12
 * Time: 11:40
 */
namespace App\Repositories;

use App\Models\CardLogo;
use App\Models\UserBankCard;

class UserBankCardRepository
{
    /**
     * 创建用户绑定的借记卡
     * @param array $params
     * @return mixed
     */
    public function create(array $params)
    {
        return UserBankCard::create($params);
    }

    /**
     * 根据id获取借记卡资料
     * @param $id
     * @return mixed
     */
    public function byId($id)
    {
        return UserBankCard::find($id);
    }

    /**
     * 根据id获取用户绑定的借记卡资料
     * @param $id
     * @return mixed
     */
    public function byUserId($id)
    {
        return UserBankCard::where('user_id', $id)->first();
    }

    /**
     * 用户是否已经存在借记卡
     * @param $userId
     * @param $bankNo
     * @return mixed
     */
    public function isExistBankNo($userId, $bankNo)
    {
        return UserBankCard::where([
            'user_id' => $userId,
            'bank_no' => $bankNo
        ])->first();
    }

    /**
     * 获取储蓄卡列表
     * @param $userId
     * @return mixed
     */
    public function getBankCardList($userId)
    {
        $bankCard = UserBankCard::where([
            'user_id' => $userId,
            'is_bind' => 1,
        ])
            ->orderby('is_default', 'desc')
            ->orderby('id', 'desc')
            ->get()->toArray();
        $data = [];
        foreach ($bankCard as $key=>$value) {
            $data[$key] = $value;
            $cardLogo = CardLogo::where('bank_code', $value['bank_code'])->first();
            if (!empty($cardLogo)) {
                $data[$key]['card_logo_url'] = getOssImageUrl($cardLogo->card_logo_url);
            } else {
                $data[$key]['card_logo_url'] = '';
            }
        }
        return $data;
    }

    /**
     * 用户对应的储蓄卡是否存在
     * @param $id
     * @return mixed
     */
    public function validateBankCardIsUser($id, $userId)
    {
        return UserBankCard::where([
            'id' => $id,
            'user_id' => $userId,
        ])->first();
    }

    /**
     * 查找默认卡
     * @param $userId
     * @return mixed
     */
    public function findDefaultCardAll($userId)
    {
        return UserBankCard::where([
            'user_id' => $userId,
            'is_bind' => 1,
        ])->get();
    }

    public function getCardList($request)
    {
        $page = $request->pageNumber - 1;
        $page = $page < 0 ? 0 : $page;
        $limit = $request->pageSize ? $request->pageSize : 50;
        $offset = $page * $limit;
        $query = UserBankCard::from('user_bank_card as c')
            ->join('card_logo as l', 'c.bank_code', '=', 'l.bank_code')
            ->select('c.*', 'l.card_logo_url')
            ->where(function ($query) use ($request) {
                if (!is_null($request->is_bind)) {
                    $query->where('c.is_bind', $request->is_bind);
                }
                if (!empty($request->keyword)) {
                    switch ($request->field) {
                        case 'user_id':
                            $query->where('c.user_id', $request->keyword);
                            break;
                        case 'id':
                            $query->where('c.id', $request->keyword);
                            break;
                        case 'bank_code':
                            $query->where('c.bank_code', 'like', '%' . $request->keyword . '%');
                            break;
                        case 'bank_no':
                            $query->where('c.bank_no', 'like', '%' . $request->keyword . '%');
                            break;
                        case 'bank_description':
                            $query->where('c.bank_description', 'like', '%' . $request->keyword . '%');
                            break;
                        case 'bank_mobile':
                            $query->where('c.bank_mobile', 'like', '%' . $request->keyword . '%');
                            break;
                        case 'card_bin':
                            $query->where('c.card_bin', 'like', '%' . $request->keyword . '%');
                            break;
                    }
                }
                if (!empty($request->start_time) && !empty($request->end_time)) {
                    $query->whereBetween('c.create_time', [strtotime($request->start_time), strtotime($request->end_time)]);
                }
            });
        $total = $query->count();
        $result = $query->latest('c.id')
            ->offset($offset)
            ->limit($limit)
            ->get();
        return ['result' => $result, 'count' => $total];
    }
    
}